package apiUtils.apis;

import android.content.Context;
import android.text.TextUtils;

import com.example.jhlibrarys.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import apiUtils.APISHelper;
import apiUtils.CallBack;
import apiUtils.models.VerifyPasswordModel;
import apiUtils.params.DESParams;
import apiUtils.params.Params;
import appUtils.AppUtil;
import appUtils.PayMD5Util;
import appUtils.PayTokenException;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

import static apiUtils.BaseAPIS.NETWORK_UNKNOWN;
import static constant.ApiConstants.DOMAIN_NAME;
import static constant.ApiConstants.SYSTEM_NAME;

/**
 * 重置手势密码
 */

public class RetrievePasswordAPI extends APISHelper {
    private static final String tag = "RetrievePasswordAPI";
    private static final String url = DOMAIN_NAME + "resetCheckAccountAndPws";

    private final Params params = new DESParams();
    private final Context mContext;
    private String userAccount;
    private String userPwd;
    private String subType;

    public RetrievePasswordAPI(Context context) {
        this.mContext = context;
    }

    public void retrievePassword(String subType, String userAccount, String userPwd, final Listener listener) {
        this.userAccount = userAccount;
        this.userPwd = userPwd;
        this.subType = subType;
        try {
            post(tag, url, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject object = new JSONObject(response);
                        String decodeMsg = desDecode(object);
                        if (!TextUtils.isEmpty(decodeMsg)) {
                            VerifyPasswordModel model = sGon.fromJson(decodeMsg, VerifyPasswordModel.class);
                            success(model, listener);
                        } else {
                            failure(-1, mContext.getString(R.string.information_error), listener);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        failure(-1, e.getMessage(), listener);
                        PayTokenException.getInstance().saveExceptionFile("RetrievePasswordAPI post json error:"
                                + e.getMessage());
                    }
                }

                @Override
                public void onError(int code) {
                    if (code == NETWORK_UNKNOWN) {
                        failure(code, AppUtil.getInstance().getContext().getString(R.string.check_network), listener);
                    } else {
                        failure(code, "", listener);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            failure(-1, e.getMessage(), listener);
            PayTokenException.getInstance().saveExceptionFile("RetrievePasswordAPI post json error:"
                    + e.getMessage());
        }
    }

    private void success(final VerifyPasswordModel model, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (null != listener) {
                    listener.onSuccess(model);
                }
            }
        });
    }

    private void failure(final int code, final String msg, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (listener != null) {
                    listener.onFailure(code, msg);
                }
            }
        });
    }

    @Override
    protected Params initParams() throws JSONException {
        params.put(mContext.getString(R.string.paramSystemName), SYSTEM_NAME);
        params.put(mContext.getString(R.string.paramSubType), subType);
        params.put("userAccount", userAccount);
        params.put("userPwd", PayMD5Util.getMD5Lower(userPwd));
        return params;
    }

    public interface Listener {
        void onSuccess(VerifyPasswordModel model);

        void onFailure(int i, String message);
    }
}
